package com.example.webdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.activity.ComponentActivity;

import java.util.List;

public class HomeActivity extends ComponentActivity implements View.OnClickListener {

    private DebugReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        findViewById(R.id.btn_web_new_tab).setOnClickListener(this);
        findViewById(R.id.btn_web_redirect).setOnClickListener(this);
        findViewById(R.id.btn_web_bd).setOnClickListener(this);

        mReceiver = new DebugReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.tv.history.secure.add");
        filter.addAction("com.tv.history.secure.del.tolauncher");
        filter.addAction("com.tv.favorite.secure.add");
        filter.addAction("com.tv.favorite.secure.del.tolauncher");
        registerReceiver(new DebugReceiver(), filter);

    }

    @Override
    public void onClick(View view) {
        int clickId = view.getId();
//        String webUrl = "https://www.baidu.com";
//        if(clickId == R.id.btn_web_new_tab){
//            webUrl = "https://www.fetchpet.com.au";
//        } else if (clickId == R.id.btn_web_redirect) {
//            webUrl = "https://www.infochoice.com.au/wiselist?ref=wiselist-app-cta";
//        }
//        Intent intent = new Intent(HomeActivity.this, WebViewActivity.class);
//        intent.putExtra(WebViewActivity.URL, webUrl);
//        startActivity(intent);
        openApp(this);
    }


    //调起大屏App
    private void openApp(Context context) {
        Intent intent = new Intent();
//        intent.setData(Uri.parse("hw://tx.video.com:8866/tvvideo?toType=7&srcId=s"/* + srcId*/));
        intent.setData(Uri.parse("hw://bili.video.com:8866/tvvideo?toType=1&srcId=cid200001"/* + srcId*/));
        handleIntent(intent, context);
    }


    private void handleIntent(Intent intent, Context context) {
        intent.setAction("com.bjhw.LoadingPage");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.setPackage(TENCENT_PACKAGENAME);//设置视频包名，要先确认包名
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
        boolean isIntentSafe = activities.size() > 0;
        if (isIntentSafe) {
            context.startActivity(intent);
        }
    }


    public static class DebugReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("DebugReceiver", "Received action: " + intent.getAction());
            Bundle extras = intent.getExtras();
            if (extras != null) {
                for (String key : extras.keySet()) {
                    Object value = extras.get(key);
                    Log.d("DebugReceiver", "Extra: " + key + " = " + value);
                }
            } else {
                Log.d("DebugReceiver", "No extras in this broadcast.");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(mReceiver);
    }
}
